package com.kchouse.mine;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.alibaba.fastjson.JSONObject;
import com.kchouse.common.API;
import com.kchouse.common.BaseActivity;
import com.kchouse.R;
import com.kchouse.common.MyHttpCycleContext;
import com.kchouse.common.SharedPreferencesUtil;
import com.kchouse.common.ToastUtils;

import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;

public class LoginActivity extends BaseActivity implements View.OnClickListener{

    private LoginActivity mContent;
    private Intent intent;
    private ImageView iv_back;
    private View ll_login;
    private Button bt_login;
    private EditText et_phone;
    private EditText et_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        mContent=this;
        intiData();
        intiView();
    }

    private void intiData() {
        intent=getIntent();
    }

    private void intiView() {
        iv_back=(ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);
        ll_login= findViewById(R.id.ll_login);
        ll_login.setOnClickListener(this);
        bt_login= (Button)findViewById(R.id.bt_login);
        bt_login.setOnClickListener(this);
        et_phone= (EditText)findViewById(R.id.et_phone);
        et_password= (EditText)findViewById(R.id.et_password);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_login://电话验证码登录
                intent.setClass(mContent,LoginPhoneActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.bt_login://密码登录
                if (TextUtils.isEmpty(et_phone.getText().toString())) {
                    ToastUtils.getInstanc(mContent).showToast("您还未填写电话号码");
                    return;
                }
                if (TextUtils.isEmpty(et_password.getText().toString())) {
                    ToastUtils.getInstanc(mContent).showToast("您还未填写密码");
                    return;
                }
                login(et_phone.getText().toString(), et_password.getText().toString());
                break;
        }
    }

    /**
     * 密码登录
     */
    private void login(String mobile, String password) {
        String url = API.LoginPassWord;
        RequestParams params = new RequestParams(new MyHttpCycleContext());//请求参数

        JSONObject ob = new JSONObject();
        ob.put("mobile", mobile);
        ob.put("password", password);
//        params.addFormDataPart("file", file);//上传单个文件
//        params.addFormDataPart("files", files);//上传多个文件
//        params.addHeader("token", token);//添加header信息
        params.applicationJson(ob);

        HttpRequest.post(url, params, new BaseHttpRequestCallback<String>() {

            //请求网络前
            @Override
            public void onStart() {
                showdialog();
            }

            @Override
            protected void onSuccess(String str) {
                Log.e("==", str);
                JSONObject json = JSONObject.parseObject(str);
                if (json.getBoolean("successful")) {
                    ToastUtils.getInstanc(mContent).showToast("登录成功");
                    String datastr = json.getString("data");
                    JSONObject data = JSONObject.parseObject(datastr);
                    String token = data.getString("token");
                    SharedPreferencesUtil.setParam(mContent, "token", token);//保存用户token
                    String userstr = data.getString("user");
                    JSONObject user = JSONObject.parseObject(userstr);
                    String mobile = user.getString("mobile");
                    SharedPreferencesUtil.setParam(mContent, "mobile", mobile);//保存用户电话
                    String id = user.getString("id");
                    SharedPreferencesUtil.setParam(mContent, "userid", id);//保存用户id
                    String qrUrl = user.getString("qrUrl");
                    SharedPreferencesUtil.setParam(mContent, "qrUrl", qrUrl);//保存用户分享链接
                    //TODO 登录时未返回积分字段


                    Intent mIntent = new Intent("LoginAction");
                    //发送广播
                    sendBroadcast(mIntent);
                    finish();
                } else {
                    String datastr = json.getString("data");
                    JSONObject data = JSONObject.parseObject(datastr);
                    ToastUtils.getInstanc(mContent).showToast(data.getString("msg"));
                }
            }

            //请求失败（服务返回非法JSON、服务器异常、网络异常）
            @Override
            public void onFailure(int errorCode, String msg) {
//                toast("网络异常~，请检查你的网络是否连接后再试");
                Log.e("==", msg);
            }

            //请求网络结束
            @Override
            public void onFinish() {
                dismissdialog();
            }
        });
    }
}
